package com.studio49.exmple.common.SMSUtils;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSON;


//import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
//import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
//import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
//import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller;
//import com.aliyuncs.http.FormatType;
//import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;

/*import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;*/

public class SendSMSTwo implements ISendSMS {
	private final static Logger log = LogManager.getLogger(SendSMSTwo.class.getName());

	private static String aliyun_sms_region_id = ""; // 阿里云regionId
	private static String aliyun_sms_access_key_id = ""; // 阿里云 accessKeyId
	private static String aliyun_sms_access_key_secret = ""; // 阿里云accessKeySecret
	private static String aliyun_sms_end_point_name = ""; // 阿里云end_point_name
	private static String aliyun_sms_product = ""; // 阿里云产品
	private static String aliyun_sms_domain = ""; // 阿里云调用短信服务平台接口域名

	private String signName; // 阿里云短信签名
	private String templateCode; // 阿里云短信模板Code
	private String paramString; // 阿里云短信模板参数字符串
	private String recNum; // 发送短信手机号

	static {
		aliyun_sms_region_id = LoadProperties.readValue("aliyun_sms_region_id");
		aliyun_sms_access_key_id = LoadProperties.readValue("aliyun_sms_access_key_id");
		aliyun_sms_access_key_secret = LoadProperties.readValue("aliyun_sms_access_key_secret");
		aliyun_sms_end_point_name = LoadProperties.readValue("aliyun_sms_end_point_name");
		aliyun_sms_product = LoadProperties.readValue("aliyun_sms_product");
		aliyun_sms_domain = LoadProperties.readValue("aliyun_sms_domain");
	}

	/**
	 * @description 方法描述
	 * @param signName
	 *            如果signName为null或者空串，则取集团短信签名; 否则取signName值
	 * @param templateId
	 *            1:验证短信模板, 2:通知短信模板, 3:推广短信模板
	 * @param paramMap
	 *            短信模板中参数名为key,参数值为value
	 * @param phoneNum
	 *            手机号数组,例：{"18888888888","19999999999"}
	 */
	public SendSMSTwo(String signName, int templateId, Map<String, String> paramMap, String[] phoneNum) {
		super();
		// 短信签名: 如果signName为null或者空串，则取集团短信签名; 否则去signName值
		this.signName = (null == signName || "".equals(signName)) ? DictSMS.SIGN_NAME_PUXIN : signName;
		// 短信模板
		switch (templateId) {
		case 1:
			this.templateCode = DictSMS.TEMPLATE_CODE_OF_VALIDATE;
			break;// 验证短信模板
		case 2:
			this.templateCode = DictSMS.TEMPLATE_CODE_OF_INFORM;
			break;// 通知短信模板
		case 3:
			this.templateCode = DictSMS.TEMPLATE_CODE_OF_PROMOTION;
			break;// 推广短信模板
		default:
			this.templateCode = DictSMS.TEMPLATE_CODE_OF_PROMOTION;
			break;// 以上三种均不匹配时，默认为 推广短信模板
		}
		// 模板参数
		this.paramString = JSON.toJSONString(paramMap);
		// 构造手机号字符串，多个手机号之间逗号分隔
		StringBuffer recNumSB = new StringBuffer();
		for (int i = 0; i < phoneNum.length; i++) {
			if (i == phoneNum.length - 1) {
				recNumSB.append(phoneNum[i]);
			} else {
				recNumSB.append(phoneNum[i] + ",");
			}
		}
		this.recNum = recNumSB.toString();
	}

	/**
	 * @description 发送短信
	 * @return 发送成功返回 true,否则返回 false
	 */
	@SuppressWarnings("finally")
	@Override
	public boolean sendSMS() {
		boolean isSendSuccess = false;
		try {
			IClientProfile profile = DefaultProfile.getProfile(aliyun_sms_region_id, aliyun_sms_access_key_id,
					aliyun_sms_access_key_secret);
			DefaultProfile.addEndpoint(aliyun_sms_end_point_name, aliyun_sms_region_id, aliyun_sms_product,
					aliyun_sms_domain);
			IAcsClient client = new DefaultAcsClient(profile);
			/*SingleSendSmsRequest request = new SingleSendSmsRequest();
			request.setSignName(signName);
			request.setTemplateCode(templateCode);
			request.setParamString(paramString);
			request.setRecNum(recNum);*/
			SendSmsRequest request = new SendSmsRequest();
			request.setSignName(signName);//签名：颖新学堂
			request.setTemplateCode(templateCode);//模板：SMS_52340097
			request.setPhoneNumbers(recNum);//手机号： 
			request.setTemplateParam(paramString); //参数 
			
			client.getAcsResponse(request);// 短信发送
			isSendSuccess = true;
		} catch (ClientException e) {
			e.printStackTrace();
			log.error("*aliyun_sms_begin*");
			log.error(e.getMessage());
			log.error(e.getErrCode());
			log.error("*aliyun_sms_end*");
			throw new RuntimeException(e);
		} finally {
			return isSendSuccess;
		}
	}
}
